#
# Copyright (c) 2019 Jiří Zárevúcky
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# - The name of the author may not be used to endorse or promote products
#   derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Read variables from config file generated by config.py,
# and version information from repository.

# Workaround for current Meson (0.51.1) not producing dependency
# on files outside source dir when just files() is used.
_config_file = configure_file(
	input: meson.build_root() / 'Makefile.config',
	output: 'Makefile.config',
	copy: true,
)

_version_file = files(meson.source_root() / 'version')

_config_variables = [
	# Uspace and kernel
	'CONFIG_BAREBONE',
	'CONFIG_BUILD_SHARED_LIBS',
	'CONFIG_DEBUG',
	'CONFIG_DEVEL_FILES',
	'CONFIG_FPU',
	'CONFIG_LINE_DEBUG',
	'CONFIG_LTO',
	'CONFIG_PCUT_SELF_TESTS',
	'CONFIG_PCUT_TESTS',
	'CONFIG_RTLD',
	'CONFIG_STRIP_BINARIES',
	'CONFIG_UBSAN',
	'CONFIG_USE_SHARED_LIBS',
	'CROSS_TARGET',
	'MACHINE',
	'OPTIMIZATION',
	'PLATFORM',
	'PROCESSOR',
	'QUADFLOAT',
	'RDFMT',

	# Kernel only
	'CONFIG_ACPI',
	'CONFIG_AM335X_TIMERS',
	'CONFIG_ASID',
	'CONFIG_ASID_FIFO',
	'CONFIG_AT_KBD',
	'CONFIG_BCM2835_MAILBOX',
	'CONFIG_DSRLNIN',
	'CONFIG_DSRLNOUT',
	'CONFIG_EGA',
	'CONFIG_FB',
	'CONFIG_GICV2',
	'CONFIG_I8042',
	'CONFIG_I8259',
	'CONFIG_IOMAP_BITMAP',
	'CONFIG_IOMAP_DUMMY',
	'CONFIG_KCONSOLE',
	'CONFIG_MAC_KBD',
	'CONFIG_MULTIBOOT',
	'CONFIG_NS16550',
	'CONFIG_OFW_PCI',
	'CONFIG_OFW_TREE',
	'CONFIG_OMAP_UART',
	'CONFIG_PAGE_HT',
	'CONFIG_PAGE_PT',
	'CONFIG_PC_KBD',
	'CONFIG_PL011_UART',
	'CONFIG_PL050',
	'CONFIG_S3C24XX_IRQC',
	'CONFIG_S3C24XX_UART',
	'CONFIG_SMP',
	'CONFIG_SOFTINT',
	'CONFIG_SRLN',
	'CONFIG_SUN_KBD',
	'CONFIG_SYMTAB',
	'CONFIG_TEST',
	'CONFIG_TRACE',
	'CONFIG_TSB',
	'CONFIG_UDEBUG',
	'CONFIG_VIA_CUDA',
	'MEMORY_MODEL',

	'UARCH',
	'KARCH',
	'BARCH',
	'GRUB_ARCH',
	'UIMAGE_OS',
	'CONFIG_COMPRESSED_INIT',
]

foreach _varname : _config_variables
	_c = run_command(grep, '^' + _varname + '\\b', _config_file)
	if _c.returncode() != 0
		# TODO: Output negative/inapplicable variables too in config, so that we can check for typos here.
		#warning('Missing configuration variable ' + _varname + ' in Makefile.config')
		set_variable(_varname, false)
		continue
	endif

	_val = _c.stdout().split('\n')[0].strip().split('=')[1].strip()
	if _val == 'y'
		_val = true
	elif _val == 'n'
		_val = false
	endif

	set_variable(_varname, _val)
	if debug_options
		message([ _varname, get_variable(_varname) ])
	endif
endforeach

# Also read version information.
_version_variables = [
	'HELENOS_COPYRIGHT',
	'HELENOS_CODENAME',
	'HELENOS_RELEASE',
]

foreach _varname : _version_variables
	_line = run_command(grep, '^' + _varname + '\\b', _version_file, check: true).stdout().strip()
	_val = _line.split('=')[1].strip()

	set_variable(_varname, _val)
	if debug_options
		message([ _varname, get_variable(_varname) ])
	endif
endforeach

# Check whether PLATFORM/MACHINE is unchanged

_profile = '@0@/@1@'.format(PLATFORM, MACHINE)
_profile_file = meson.build_root() / 'prev_profile'

_cmd = run_command('cat', _profile_file)
if _cmd.returncode() == 0
	_prev_profile = _cmd.stdout().strip()

	if _prev_profile != _profile
		error('This build directory was initially configured for ' + _prev_profile +
			', but now is configured for ' + _profile + '. This is not supported.')
	endif
else
	run_command('sh', '-c', 'echo "' + _profile + '" > ' + _profile_file, check: true)
endif
